import Cookies from "js-cookie";
export default {
    namespaced: true,
    state: {
        tagsLists: [
            {
                path: '/home',
                name: 'home',
                label: '首页',
                icon: 'el-icon-s-home',
                url: 'HomeView.vue'
            },
        ],
        menu: []
    },
    mutations: {
        // 更新面包屑数据
        renewTagsLists(state, val) {
            // console.log(val);
            if (val.name != 'home') {
                const index = state.tagsLists.findIndex(item => item.name === val.name);
                // 如果不存在
                if (index == -1) {
                    state.tagsLists.push(
                        val
                    )
                }
            }
        },
        closeTags(state, item) {
            // console.log(item);
            const index = state.tagsLists.findIndex(val => val.name === item.name);
            state.tagsLists.splice(index, 1);
        },
        getMenuData(state, value) {
            state.menu = value;
            Cookies.set('menu', JSON.stringify(value));
        },
        addMenu(state, router) {
            // console.log(router);
            if (!Cookies.get('menu')) return;
            const menu = JSON.parse(Cookies.get('menu'));
            state.menu = menu;
            const menuArray = [];
            menu.forEach(item => {
                // console.log(item);
                if (item.children) {
                    item.children = item.children.map(i => {
                        // console.log(item);
                        i.component = () => import(`@/views/${i.url}`);
                        return i;
                    })
                    menuArray.push(...item.children)
                } else {
                    item.component = () => import(`@/views/${item.url}`);
                    menuArray.push(item)
                }
            })
            // console.log(menuArray);
            menuArray.forEach(item => {
                router.addRoute('main', item)
            })
            Cookies.set('menu', JSON.stringify(menuArray));
            // console.log(JSON.parse(Cookies.get('menu')));
        }
    }
}